package com.example.utils;

import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * @project: shareCar
 * @description: 随机生成用户名工具类
 * @author: cjq
 * @create: 2022-08-18 11:39
 **/
public class RandomNameUtitls {
    /**
     * 自动生成中文名称
     * @param len
     * @return
     */
    public static String getRandomJianHan(int len) {
        String ret = "";
        for (int i = 0; i<len;i++){
            String str = null;
            //定义高低位
            int hightPos, lowPos;
            Random random = new Random();
            //获取高位置
            hightPos = (176+Math.abs(random.nextInt(39)));
            //获取低位置
            lowPos = (161+Math.abs(random.nextInt(93)));
            byte[] b = new byte[2];
            b[0] = (new Integer(hightPos).byteValue());
            b[1] = (new Integer(lowPos).byteValue());
            try {
                //转成中文
                str = new String(b,"GBK");
            }catch (UnsupportedEncodingException exception){
                exception.printStackTrace();
            }
            ret += str;
        }
        return ret;
    }

    /**
     * 参数length，表示生成几位随机数
     * @param length
     * @return
     */
    public static String getStringRandom(int length){
        String val = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for (int i=0;i<length;i++){
            String s = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(s)){
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26)+temp);
            }else if ("num".equalsIgnoreCase(s)){
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
}
